//
// GPSMapEdit
// (c) Konstantin Galichsky (kg@geopainting.com), 2002-2007
//
// Registry access.
//

# include "StdAfx.h"
# include "Settings.h"
# include "Globals.h"

static HKEY g_hkeySettings;

void InitRegistryAccess () {
	DWORD dwDisposition;
	if (
		::RegCreateKeyEx (
			HKEY_CURRENT_USER, "Software\\Geopainting.com\\GPSMapEdit", 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, & g_hkeySettings, & dwDisposition
		) != ERROR_SUCCESS
	)
		g_hkeySettings = NULL;
}

bool IsSettingDefined (const char * _strName) {
	if (! g_hkeySettings) {
		InitRegistryAccess ();
		if (! g_hkeySettings)
			return false;
	}

	DWORD dwType = 0;
	DWORD dwSize = 0;
	if (
		::RegQueryValueEx (
			g_hkeySettings, _strName, NULL, & dwType, NULL, & dwSize
		) != ERROR_SUCCESS
	)
		return false;

	return true;
}

size_t GetSettingLen (const char * _strName) {
	if (! g_hkeySettings) {
		InitRegistryAccess ();
		if (! g_hkeySettings)
			return 0;
	}

	DWORD dwType = 0;
	DWORD dwSize = 0;
	if (
		::RegQueryValueEx (
			g_hkeySettings, _strName, NULL, & dwType, NULL, & dwSize
		) != ERROR_SUCCESS ||
		(dwType != REG_SZ && dwType != REG_BINARY)
	)
		return 0;

	return dwSize;
}

DWORD GetSetting (const char * _strName, DWORD _dwDefault) {
	if (! g_hkeySettings) {
		InitRegistryAccess ();
		if (! g_hkeySettings)
			return _dwDefault;
	}

	DWORD dwRet = 0;
	DWORD dwType = 0;
	DWORD dwSize = sizeof (DWORD);
	if (
		::RegQueryValueEx (
			g_hkeySettings, _strName, NULL, & dwType, reinterpret_cast<BYTE *> (& dwRet), & dwSize
		) != ERROR_SUCCESS ||
		dwType != REG_DWORD ||
		dwSize != sizeof (DWORD)
	)
		return _dwDefault;

	return dwRet;
}

bool GetSetting (const char * _strName, bool _bDefault) {
	return GetSetting (_strName, _bDefault ? (DWORD) 1 : (DWORD) 0) != 0;
}

void GetSetting (const char * _strName, char * _strRet, size_t _cSize, const char * _strDefault) {
	if (! g_hkeySettings) {
		InitRegistryAccess ();
		if (! g_hkeySettings) {
			::strcpy (_strRet, _strDefault);
			return;
		}
	}

	if (_cSize > 0)
		_strRet [0] = '\0';

	DWORD dwType = 0;
	DWORD dwSize = _cSize;
	if (
		::RegQueryValueEx (
			g_hkeySettings, _strName, NULL, & dwType, reinterpret_cast<BYTE *> (_strRet), & dwSize
		) != ERROR_SUCCESS ||
		dwType != REG_SZ
	)
		::strncpy (_strRet, _strDefault == NULL ? "" : _strDefault, _cSize);
}

size_t GetSetting (const char * _strName, void * _pRet, size_t _cMaxSizeInBytes) {
	if (! g_hkeySettings)
		InitRegistryAccess ();

	DWORD dwType = 0;
	DWORD dwSize = _cMaxSizeInBytes;
	if (
		::RegQueryValueEx (
			g_hkeySettings, _strName, NULL, & dwType, reinterpret_cast<BYTE *> (_pRet), & dwSize
		) != ERROR_SUCCESS ||
		dwType != REG_BINARY
	)
		return 0;

	return dwSize;
}

void SaveSetting (const char * _strName, DWORD _dwValue) {
	if (! g_hkeySettings) {
		InitRegistryAccess ();
		if (! g_hkeySettings)
			return;
	}

	::RegSetValueEx (g_hkeySettings, _strName, 0, REG_DWORD, reinterpret_cast<const BYTE *> (& _dwValue), sizeof (DWORD));
}

void SaveSetting (const char * _strName, bool _bValue) {
	SaveSetting (_strName, _bValue ? (DWORD) 1 : (DWORD) 0);
}

void SaveSetting (const char * _strName, const char * _strValue) {
	if (! g_hkeySettings) {
		InitRegistryAccess ();
		if (! g_hkeySettings)
			return;
	}

	::RegSetValueEx (g_hkeySettings, _strName, 0, REG_SZ, reinterpret_cast<const BYTE *> (_strValue), ::strlen (_strValue) + 1);
}

void SaveSetting (const char * _strName, const void * _pValue, size_t _cSizeInBytes) {
	if (! g_hkeySettings) {
		InitRegistryAccess ();
		if (! g_hkeySettings)
			return;
	}

	::RegSetValueEx (g_hkeySettings, _strName, 0, REG_BINARY, reinterpret_cast<const BYTE *> (_pValue), _cSizeInBytes);
}
